#include "modifyuserdialog.h"
#include "ui_modifyuserdialog.h"
#include "usersql.h"
#include "mainwindow.h"

int uid;

modifyUserDialog::modifyUserDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::modifyUserDialog)
{
    ui->setupUi(this);
}

modifyUserDialog::~modifyUserDialog()
{
    delete ui;
}

modifyUserDialog::load(QString id, QString name, QString sex, QString idcard, QString address, QString phone)
{
    uid = id.toInt();
    ui->le_name->setText(name);
    if(QString::compare(sex, "男") == 0)
    {
        ui->cb_sex->setCurrentIndex(0);
    }
    else
    {
        ui->cb_sex->setCurrentIndex(1);
    }
    ui->le_idcard->setText(idcard);
    ui->le_address->setText(address);
    ui->le_phone->setText(phone);
}

void modifyUserDialog::on_btn_cancel_clicked()
{
    this->hide();
}

void modifyUserDialog::on_btn_save_clicked()
{
    User user;
    user.id = uid;
    user.name = ui->le_name->text();
    user.sex = ui->cb_sex->currentText();
    user.idcard = ui->le_idcard->text();
    user.address = ui->le_address->text();
    user.phone = ui->le_phone->text();
    auto ptr2 = MainWindow::getinstance();
    ptr2->us.load(user);
    ptr2->updateUser(user);
    this->hide();
    ptr2 = nullptr;
}
